package com.lwm.newdate;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 * @author liweimin
 * @date 2020/9/15 15:33
 * @description
 */
public class NewDateTest {
    @Test
    public void test01() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
    }

    @Test
    public void test02() {
        LocalDateTime time = LocalDateTime.of(2020, 9, 16, 12, 30, 30);
        System.out.println(time);
    }

    @Test
    public void test03() {
        LocalDateTime time = LocalDateTime.of(2020, 9, 16, 12, 30, 30);
        LocalDateTime time1 = time.plusYears(23);
        LocalDateTime localDateTime = time1.minusDays(789);
        System.out.println(localDateTime);
        System.out.println(localDateTime.getDayOfYear());
    }

    @Test
    public void test04() {
        /**
         * 默认获取世界协调时间（UTC）格林威治时间  (UNIX)
         */
        Instant now = Instant.now();
        System.out.println(now);
        OffsetDateTime time = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(time);
        System.out.println(now.toEpochMilli());
    }

    @Test
    public void test05() throws InterruptedException {
        /**
         * 计算时间之间的间隔 Duration
         */

        Instant now = Instant.now();
        Thread.sleep(124);
        System.out.println("展示now与now1之间的时间间隔为：");
        Instant now1 = Instant.now();
        Duration between = Duration.between(now, now1);
        System.out.println(between.toMillis());

        LocalTime now2 = LocalTime.now();
        Thread.sleep(1000);
        LocalTime now3 = LocalTime.now();
        Duration between1 = Duration.between(now2, now3);
        System.out.println(between1.toMillis());
    }

    @Test
    public void test06() {
        /**
         * 计算日期之间的间隔 Period
         */
        LocalDate now = LocalDate.now();
        LocalDate of = LocalDate.of(2005, 12, 3);
        Period between = Period.between(now, of);
        System.out.println(between.toTotalMonths());

    }

    @Test
    public void test07() {
        /**
         * 时间矫正器
         */
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.withDayOfYear(10);
        System.out.println(localDateTime);
        LocalDateTime localDateTime1 = now.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(localDateTime1);
        LocalDateTime time = now.with(e -> {
            LocalDateTime localDateTime2 = (LocalDateTime) e;
            LocalDateTime localDateTime3;
            localDateTime3 = null;
            DayOfWeek dayOfWeek = localDateTime2.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                localDateTime3 = localDateTime2.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SUNDAY)) {
                localDateTime3 = localDateTime2.plusDays(1);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                localDateTime3 = localDateTime2.plusDays(2);
            } else {
                localDateTime3 = localDateTime2.plusDays(1);
            }
            return localDateTime3;
        });
        System.out.println(time);
    }
    @Test
    public void test08(){
        DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(isoDateTime);
        System.out.println(format);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(now.format(dateTimeFormatter));
      LocalDateTime parse = LocalDateTime.parse("2020年09月15日 16:02:04",dateTimeFormatter);
       System.out.println(parse);
    }
    @Test
    public void test09(){
        /**
         * Zone*
         */
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        availableZoneIds.forEach(System.out::println);
        LocalDateTime now = LocalDateTime.now();
        ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Aden"));
        System.out.println(zonedDateTime);
    }
}
